package com.lds.erp.util;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;

import com.lds.erp.mch.mch.ApiCacheConstants;

/**
 * @author hongsir
 * @apiNote 登录账号相关的工具类, 取值, 存值等
 * @date 2018/9/29 上午11:05
 */
public class CacheUtils {

	private static RedisTemplate redisTemplate;

	private static RedisTemplate getRedisInstance() {
		if (ObjectUtils.isEmpty(redisTemplate)) {
			redisTemplate = (RedisTemplate) SpringContextUtils.getBean("redisTemplate");
		}
		return redisTemplate;
	}

	/**
	 * 根据用户ID,强制踢人 用户缓存key规则与cacheAccount()保持一致即可 缓存前缀+用户id+'_' String key =
	 * CacheConstants.REDIS_USER_KEY_PREFIX + account.getUserId() + "_" +
	 * account.getCacheKey();
	 *
	 * @param userIdList
	 */
	public static void forceRemove(List<String> userIdList) {
		if (!ObjectUtils.isEmpty(userIdList)) {
			Set<String> keys = new HashSet<>();
			for (String userId : userIdList) {
				Set<String> _keys = getRedisInstance().keys(ApiCacheConstants.MCH_REDIS_USER_KEY_PREFIX + userId);
				keys.addAll(_keys);
			}
			getRedisInstance().delete(keys);
		}
	}

}
